package ai.koog.agents.core.feature.model.events

import ai.koog.agents.core.feature.model.AIAgentError
import kotlinx.datetime.Clock
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonElement

/**
 * Represents an event triggered when the execution of a specific subgraph starts.
 *
 * This event captures the initiation of a subgraph execution, including details
 * such as the unique run identifier, the name of the subgraph, and the input data
 * provided to the subgraph.
 *
 * @property runId Unique identifier for the subgraph run;
 * @property subgraphName The name of the subgraph being executed;
 * @property input The input data provided to the subgraph;
 * @property timestamp The timestamp of the event, in milliseconds since the Unix epoch.
 */
@Serializable
public data class SubgraphExecutionStartingEvent(
    val runId: String,
    val subgraphName: String,
    val input: JsonElement?,
    override val timestamp: Long = Clock.System.now().toEpochMilliseconds(),
) : DefinedFeatureEvent()

/**
 * Represents an event triggered when the execution of a specific subgraph completes.
 *
 * @property runId Unique identifier for the subgraph run;
 * @property subgraphName The name of the subgraph being executed;
 * @property input The input data provided to the subgraph;
 * @property output The output data generated by the subgraph;
 * @property timestamp The timestamp of the event, in milliseconds since the Unix epoch.
 */
@Serializable
public data class SubgraphExecutionCompletedEvent(
    val runId: String,
    val subgraphName: String,
    val input: JsonElement?,
    val output: JsonElement?,
    override val timestamp: Long = Clock.System.now().toEpochMilliseconds(),
) : DefinedFeatureEvent()

/**
 * Represents an event triggered when the execution of a specific subgraph fails.
 *
 * @property runId Unique identifier for the subgraph run;
 * @property subgraphName The name of the subgraph being executed;
 * @property input The input data provided to the subgraph;
 * @property error The error that occurred during the subgraph execution;
 * @property timestamp The timestamp of the event, in milliseconds since the Unix epoch.
 */
@Serializable
public data class SubgraphExecutionFailedEvent(
    val runId: String,
    val subgraphName: String,
    val input: JsonElement?,
    val error: AIAgentError,
    override val timestamp: Long = Clock.System.now().toEpochMilliseconds(),
) : DefinedFeatureEvent()
